package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.cardbaobao.cardbabyclient.ui.activity.CreditCardRaidersActivity;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.CreditCardRaidersLayout;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;

import java.util.List;

/**
 * Created by Jiangzx on 19:19.
 * 热门专题
 */

public class HotTopicDelegateAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {
    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }
    private LayoutHelper layoutHelper;
    private List<CreditCardRaidersLayout.HotTopic> data;
    public HotTopicDelegateAdapter(LayoutHelper layoutHelper,List<CreditCardRaidersLayout.HotTopic> data) {
        this.layoutHelper=layoutHelper;
        this.data=data;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return layoutHelper;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context=parent.getContext();
        TextView textView = new TextView(context);
        RecyclerView.LayoutParams params=
                new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                        DensityUtil.dp2px(context,46.0f));
        textView.setBackgroundColor(0xffffffff);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(0xff6cc2ff);
        textView.setMaxLines(1);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,13.0f);
        textView.setLayoutParams(params);
        return new HotTopicHolder(textView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        HotTopicHolder topicHolder= (HotTopicHolder) holder;
        topicHolder.textView.setText("#"+data.get(position).getName()+"#");
        topicHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clickedCallback!=null){
                    clickedCallback.onRecycleItemClicked(position);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return CreditCardRaidersActivity.RAIDERS_HOT_TOPIC;
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    private class HotTopicHolder extends RecyclerView.ViewHolder{
        private TextView textView;
        private HotTopicHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView;
        }
    }
}
